package com.itsm.utils;


import com.itsm.entity.Process;
import com.itsm.service.SendService;
import com.itsm.service.mapper.ProcessMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;

@Component
public class TaskUtils {
    @Autowired
    private ProcessMapper processMapper;
    @Autowired
    private SendService sendService;
    @Scheduled(cron = "0 0 8,11,14,17,20 ? * 1,2,3,4,5,6 ") // cron表达式：
    public void doTask() throws IOException {

        List<Process> undone = processMapper.getUndone();
        List<Process> undistributed = processMapper.getUndistributed();
        List<Process> unaccepted = processMapper.getUnaccepted();



        int size1 = undone.size();
        int size2 = undistributed.size();
        int size3 = unaccepted.size();


        //未完成的工单
        if(size1>0){
            for(int i = 0;i<size1;i++){
                Process process1 = undone.get(i);
                sendService.sendEnUndone(process1);

            }
        }

        if(size2>0){
            for(int i = 0;i<size2;i++){
                Process process2 = undistributed.get(i);
                sendService.sendUndistributed(process2);
            }
        }


        if(size3>0){
            for(int i = 0;i<size3;i++){
                Process process3 = unaccepted.get(i);
                sendService.sendUnaccepted(process3);
            }
        }


    }
    @Scheduled(cron = "0 0 11,17 ? * 1,2,3,4,5,6 ") // cron表达式：
    public void doTask1() throws IOException {
        List<Process> unApprove = processMapper.getUnApprove();
        int size4 = unApprove.size();
        if(size4>0){
            for(int i = 0;i<size4;i++){
                Process process4 = unApprove.get(i);
                String approver = processMapper.getUnApprove1(process4.getProcessNo());
                sendService.sendSup(process4,approver);
            }
        }

    }

}
